package com.loongtech.bi.action.bireport;

import com.loongtech.bi.action.BaseBiSearchAction;
import com.loongtech.bi.manager.realTime.BossRealTimeManager;
import com.loongtech.bi.manager.report.GameReportManager;
import com.loongtech.bi.manager.report.GameReportManager.GameReportData;
import com.loongtech.bi.manager.system.SysRoleChannelManager;
import com.loongtech.bi.manager.system.SysUserManager;
import com.loongtech.core.util.Time;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * 运营报表-平台/渠道/区服Action
 * @author Marin
 * @date 2015年09月17日
 */
@Controller
@Scope("session")
@RequestMapping("/bi/gameReport")
public class GameReportBiAction extends BaseBiSearchAction {
	private static final long serialVersionUID = 1L;
    private String dateRadio = "7";  // 切换的天数：7=近7天。15=近15天。30=近30天，0=自选时间
    private Date dateStart = new Date(Time.getBeginningOfDay(Time.currentTime() - 7 * Time.DAY));  // 自选时间：开始时间
    private Date dateEnd = new Date(Time.getBeginningOfDay(Time.currentTime() - Time.DAY));  // 自选时间：结束时间

    private String selectedPlatform = "0";  // 0=全部平台;2=ios正版;1=ios越狱;3=安卓
    private String selectedServer = "all";  // all=全部平台;ioszb=ios正版;hf=混服;yyb=应用宝;yhlm=硬核联盟

    private List<GameReportData> platformDataList;  // 平台数据，包括平台汇总和平台详细，其中平台详细是平台汇总里GameReportData对象的togglerDatas属性。平台汇总数据也用于供前端显示
	private List<GameReportData> detailChannelDataList;  // 详细渠道数据
    private List<GameReportData> detailServerDataList;  // 详细区服数据

    private List<GameReportData> detailPlatformDisplayData;  // 供前端显示的平台详细数据
    private List<GameReportData> channelDisplayDataList;  // 供前端显示的渠道汇总数据
    private List<GameReportData> detailChannelDisplayDataList;  // 供前端显示的渠道详细数据
    private List<GameReportData> detailServerDisplayDataList;  // 供前端显示的区服详细数据

    private List<GameReportData> cpsDataList;  // 供前端cps汇总数据

    private Integer uid;

	@Autowired
	GameReportManager gameReportManager;

	@Autowired
    SysRoleChannelManager sysRoleChannelManager;

    @Autowired
    private SysUserManager sysUserManager;

    @RequestMapping(value = "search.do", method = {RequestMethod.GET, RequestMethod.POST})
    public void search(HttpServletRequest request, HttpServletResponse response, @RequestParam String dstart, @RequestParam String dend, String callback) {
        Map<String, Object> result = new HashMap<>();
        try {
            Date startDay = Time.formatDate(dstart, Time.TIMEPATTERN_SIMPLE);
            Date endDay = Time.formatDate(dend, Time.TIMEPATTERN_SIMPLE);

            platformDataList = new ArrayList<>();
            detailChannelDataList = new ArrayList<>();
            detailServerDataList = new ArrayList<>();
            detailPlatformDisplayData = new ArrayList<>();
            channelDisplayDataList = new ArrayList<>();
            detailChannelDisplayDataList = new ArrayList<>();
            detailServerDisplayDataList = new ArrayList<>();

            List<GameReportData> allData = gameReportManager.getAllGameReportData(startDay, endDay);
            if (allData != null && allData.size() > 0) {
                gameReportManager.updateData(allData, platformDataList, detailChannelDataList, detailServerDataList);
                //===
                detailPlatformDisplayData.clear();
                channelDisplayDataList.clear();
                detailChannelDisplayDataList.clear();

                for (GameReportData p: platformDataList) {  // 取对应平台的平台详细数据
                    if (selectedPlatform.equals(p.getPlatformId())) {
                        detailPlatformDisplayData.addAll(p.getTogglerDatas());
                        break;
                    }
                }

                for (GameReportData p: detailChannelDataList) {  // 取对应平台的渠道汇总和详细数据
                    if ("0".equals(selectedPlatform)) {
                        channelDisplayDataList.add(p);
                        detailChannelDisplayDataList.addAll(p.getTogglerDatas());
                    } else {
                        if (selectedPlatform.equals(p.getPlatformId())) {
                            channelDisplayDataList.add(p);
                            detailChannelDisplayDataList.addAll(p.getTogglerDatas());
                        }
                    }
                }
            }

            result.put("res", "0");
            result.put("platformDataList", platformDataList);
            result.put("detailPlatformDisplayData", detailPlatformDisplayData);
            result.put("channelDisplayDataList", channelDisplayDataList);
            result.put("detailChannelDisplayDataList", detailChannelDisplayDataList);

            printToJson(response, result, callback);
        } catch (Exception e) {
            e.printStackTrace();
            result.put("res", "1");
            result.put("msg", e.getMessage());
            printToJson(response, result, callback);
        }
	}

    public void getChannelDatasByUser() {
        cpsDataList = new ArrayList<>();
        List<GameReportData> allList = getChannelDisplayDataList();
        Set<String> selectedSet = sysRoleChannelManager.getChannelSetByUserId(uid);
        if (null == selectedSet || selectedSet.isEmpty() || null == allList || allList.isEmpty()) {
            return;
        }

        for (GameReportData info : allList) {
            if (selectedSet.contains(info.getChannelId())) {
                cpsDataList.add(info);
            }
        }
    }

    // 切换标签页
    @RequestMapping(value = "updatePlatform.do", method = {RequestMethod.GET, RequestMethod.POST})
    public void updatePlatform(HttpServletRequest request, HttpServletResponse response, @RequestParam String platform, String callback) {
        Map<String, Object> result = new HashMap<>();
        try {
            detailPlatformDisplayData.clear();
            channelDisplayDataList.clear();
            detailChannelDisplayDataList.clear();

            for (GameReportData p: platformDataList) {  // 取对应平台的平台详细数据
                if (platform.equals(p.getPlatformId())) {
                    detailPlatformDisplayData.addAll(p.getTogglerDatas());
                    break;
                }
            }

            for (GameReportData p: detailChannelDataList) {  // 取对应平台的渠道汇总和详细数据
                if ("0".equals(platform)) {
                    channelDisplayDataList.add(p);
                    detailChannelDisplayDataList.addAll(p.getTogglerDatas());
                } else {
                    if (platform.equals(p.getPlatformId())) {
                        channelDisplayDataList.add(p);
                        detailChannelDisplayDataList.addAll(p.getTogglerDatas());
                    }
                }
            }
            getChannelDatasByUser();

            result.put("detailPlatformDisplayData", detailPlatformDisplayData);

            printToJson(response, result, callback);
        } catch (Exception e) {
            e.printStackTrace();
            result.put("res", "1");
            result.put("msg", e.getMessage());
            printToJson(response, result, callback);
        }
    }

    @RequestMapping(value = "searchCPS.do", method = {RequestMethod.GET, RequestMethod.POST})
    public void searchCPS(HttpServletRequest request, HttpServletResponse response, @RequestParam String dstart, @RequestParam String dend, @RequestParam String platform, @RequestParam String uid, String callback) {
        Map<String, Object> result = new HashMap<>();
        try {
            Date startDay = Time.formatDate(dstart, Time.TIMEPATTERN_SIMPLE);
            Date endDay = Time.formatDate(dend, Time.TIMEPATTERN_SIMPLE);

            Integer userid = sysUserManager.getUserIdByName(uid);
            if (null == userid) {
                result.put("res", 1);
                result.put("msg", "帐户错误，请联系管理员!");
                printToJson(response, result, callback);
                return;
            }

            this.uid = userid;
            this.selectedPlatform = platform;

            platformDataList = new ArrayList<>();
            detailChannelDataList = new ArrayList<>();
            detailServerDataList = new ArrayList<>();
            detailPlatformDisplayData = new ArrayList<>();
            channelDisplayDataList = new ArrayList<>();
            detailChannelDisplayDataList = new ArrayList<>();
            detailServerDisplayDataList = new ArrayList<>();
            cpsDataList = new ArrayList<>();

            List<GameReportData> allData = gameReportManager.getAllGameReportData(startDay, endDay);
            if (allData != null && allData.size() > 0) {
                gameReportManager.updateData(allData, platformDataList, detailChannelDataList, detailServerDataList);

                detailPlatformDisplayData.clear();
                channelDisplayDataList.clear();
                detailChannelDisplayDataList.clear();

                for (GameReportData p: platformDataList) {  // 取对应平台的平台详细数据
                    if (selectedPlatform.equals(p.getPlatformId())) {
                        detailPlatformDisplayData.addAll(p.getTogglerDatas());
                        break;
                    }
                }

                for (GameReportData p: detailChannelDataList) {  // 取对应平台的渠道汇总和详细数据
                    if ("0".equals(selectedPlatform)) {
                        channelDisplayDataList.add(p);
                        detailChannelDisplayDataList.addAll(p.getTogglerDatas());
                    } else {
                        if (selectedPlatform.equals(p.getPlatformId())) {
                            channelDisplayDataList.add(p);
                            detailChannelDisplayDataList.addAll(p.getTogglerDatas());
                        }
                    }
                }

                getChannelDatasByUser();
            }

            result.put("res", "0");
            result.put("cpsDataList", cpsDataList);

            printToJson(response, result, callback);
        } catch (Exception e) {
            e.printStackTrace();
            result.put("res", "1");
            result.put("msg", e.getMessage());
            printToJson(response, result, callback);
        }
    }

    @RequestMapping(value = "searchServer.do", method = {RequestMethod.GET, RequestMethod.POST})
    public void searchServer(HttpServletRequest request, HttpServletResponse response, @RequestParam String dstart, @RequestParam String dend, String callback) {
        Map<String, Object> result = new HashMap<>();
        try {
            Date startDay = Time.formatDate(dstart, Time.TIMEPATTERN_SIMPLE);
            Date endDay = Time.formatDate(dend, Time.TIMEPATTERN_SIMPLE);

            platformDataList = new ArrayList<>();
            detailChannelDataList = new ArrayList<>();
            detailServerDataList = new ArrayList<>();
            detailPlatformDisplayData = new ArrayList<>();
            channelDisplayDataList = new ArrayList<>();
            detailChannelDisplayDataList = new ArrayList<>();
            detailServerDisplayDataList = new ArrayList<>();

            List<GameReportData> allData = gameReportManager.getAllGameReportData(startDay, endDay);
            if (allData != null && allData.size() > 0) {
                gameReportManager.updateData(allData, platformDataList, detailChannelDataList, detailServerDataList);
                detailPlatformDisplayData.clear();
                channelDisplayDataList.clear();
                detailChannelDisplayDataList.clear();

                for (GameReportData p: platformDataList) {  // 取对应平台的平台详细数据
                    if (selectedPlatform.equals(p.getPlatformId())) {
                        detailPlatformDisplayData.addAll(p.getTogglerDatas());
                        break;
                    }
                }

                for (GameReportData p: detailChannelDataList) {  // 取对应平台的渠道汇总和详细数据
                    if ("0".equals(selectedPlatform)) {
                        channelDisplayDataList.add(p);
                        detailChannelDisplayDataList.addAll(p.getTogglerDatas());
                    } else {
                        if (selectedPlatform.equals(p.getPlatformId())) {
                            channelDisplayDataList.add(p);
                            detailChannelDisplayDataList.addAll(p.getTogglerDatas());
                        }
                    }
                }

                selectServer();
            }

            result.put("res", "0");
            result.put("detailServerDisplayDataList", detailServerDisplayDataList);

            printToJson(response, result, callback);
        } catch (Exception e) {
            e.printStackTrace();
            result.put("res", "1");
            result.put("msg", e.getMessage());
            printToJson(response, result, callback);
        }
    }

    public void selectServer() {
        detailServerDisplayDataList.clear();

        for (GameReportData p: detailServerDataList) {
            if ("all".equals(selectedServer)) {
                detailServerDisplayDataList.addAll(p.getTogglerDatas());
            } else {
                if (p.getServerTypeName().equals(BossRealTimeManager.INDEX2NAME.get(selectedServer))) {
                    detailServerDisplayDataList.addAll(p.getTogglerDatas());
                }
            }
        }
    }

    /**
     * 切换天数
     */
    public void radioChange() {
        int stayDay = "0".equals(dateRadio) ? 2 : Integer.parseInt(dateRadio);
        dateStart = new Date(Time.getBeginningOfDay(Time.currentTime() - stayDay * Time.DAY));

        search();
    }

    /**
     * 划动天数
     */
    public void spinnerChange() {
        search();
    }

    @Override
    public void search() {

    }

    @Override
    protected String csvContent() {
        return null;
    }

    @Override
    protected String csvFileName() {
        return null;
    }

    public Date getDateStart() {
		return dateStart;
	}

	public void setDateStart(Date dateStart) {
		this.dateStart = dateStart;
	}

	public Date getDateEnd() {
		return dateEnd;
	}

	public void setDateEnd(Date dateEnd) {
		this.dateEnd = dateEnd;
	}

    public List<GameReportData> getPlatformDataList() {
        return platformDataList;
    }

    public void setPlatformDataList(List<GameReportData> platformDataList) {
        this.platformDataList = platformDataList;
    }

    public List<GameReportData> getDetailChannelDataList() {
        return detailChannelDataList;
    }

    public void setDetailChannelDataList(List<GameReportData> detailChannelDataList) {
        this.detailChannelDataList = detailChannelDataList;
    }

    public String getSelectedPlatform() {
        return selectedPlatform;
    }

    public void setSelectedPlatform(String selectedPlatform) {
        this.selectedPlatform = selectedPlatform;
    }

    public List<GameReportData> getDetailPlatformDisplayData() {
        return detailPlatformDisplayData;
    }

    public void setDetailPlatformDisplayData(List<GameReportData> detailPlatformDisplayData) {
        this.detailPlatformDisplayData = detailPlatformDisplayData;
    }

    public List<GameReportData> getDetailChannelDisplayDataList() {
        return detailChannelDisplayDataList;
    }

    public void setDetailChannelDisplayDataList(List<GameReportData> detailChannelDisplayDataList) {
        this.detailChannelDisplayDataList = detailChannelDisplayDataList;
    }

    public String getDateRadio() {
        return dateRadio;
    }

    public void setDateRadio(String dateRadio) {
        this.dateRadio = dateRadio;
    }

    public List<GameReportData> getChannelDisplayDataList() {
        return channelDisplayDataList;
    }

    public void setChannelDisplayDataList(List<GameReportData> channelDisplayDataList) {
        this.channelDisplayDataList = channelDisplayDataList;
    }

    public List<GameReportData> getDetailServerDataList() {
        return detailServerDataList;
    }

    public void setDetailServerDataList(List<GameReportData> detailServerDataList) {
        this.detailServerDataList = detailServerDataList;
    }

    public List<GameReportData> getDetailServerDisplayDataList() {
        return detailServerDisplayDataList;
    }

    public void setDetailServerDisplayDataList(List<GameReportData> detailServerDisplayDataList) {
        this.detailServerDisplayDataList = detailServerDisplayDataList;
    }

    public String getSelectedServer() {
        return selectedServer;
    }

    public void setSelectedServer(String selectedServer) {
        this.selectedServer = selectedServer;
    }

    public List<GameReportData> getCpsDataList() {
        return cpsDataList;
    }

    public void setCpsDataList(List<GameReportData> cpsDataList) {
        this.cpsDataList = cpsDataList;
    }

    public Integer getUid() {
        return uid;
    }

    public void setUid(Integer uid) {
        this.uid = uid;
    }
}
